package com.rongmai.web.bo;

import com.rongmai.db.dto.NewsDTO;
import com.rongmai.db.mapper.NewsMapper;
import com.rongmai.db.model.News;
import com.rongmai.db.vo.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class NewsBo {

    @Resource
    private NewsMapper newsMapper;

    public Page<News> page(NewsDTO newsDTO) {
        if (newsDTO.getPageNo() != 0) {
            newsDTO.setPageNo(newsDTO.getPageNo() - 1);
        }
        if (newsDTO.getPageSize() == 0) {
            newsDTO.setPageSize(10);
        }
        Page<News> result = new Page<>();
        List<News> news = newsMapper.pageList(newsDTO);
        int count = newsMapper.count(newsDTO);
        result.setList(news);
        result.setTotal(count);
        return result;
    }

    public News get(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }
}
